package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.AddressBook;
import com.itheima.reggie.service.AddressBookService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 地址管理
 */
@RestController
@RequestMapping("addressBook")
@Slf4j
@Api(value = "地址簿")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    /**
     * 查询当前用户 地址信息
     * @return 查询结果
     */
    @GetMapping("list")
    public R<List<AddressBook>> list(){
        //获取当前登录的 用户Id
        Long userId = BaseContext.getCurrentId();
        log.info("查询id为{} 的地址", userId);
        LambdaQueryWrapper<AddressBook> wrapper = Wrappers.lambdaQuery(AddressBook.class)
                .eq(AddressBook::getUserId, userId)
                .orderByDesc(AddressBook::getUpdateTime);
        //查询数据
        List<AddressBook> addressBookList = addressBookService.list(wrapper);
        return R.success(addressBookList);
    }

    /**
     * 根据id查询地址信息
     * @param id 地址表id
     * @return 查询结果
     */
    @GetMapping("/{id}")
    public R<AddressBook> findAddressById(@PathVariable Long id){
        log.info("查询id为:{}的地址信息", id);
        AddressBook addressBook = addressBookService.getById(id);

        return R.success(addressBook);
    }

    /**
     * 查询当前用户 默认地址
     * @return 返回默认地址信息
     */
    @GetMapping("/default")
    public R<AddressBook> defaultAddress(){
        Long userId = BaseContext.getCurrentId();
        log.info("查询当前用户{} 默认地址",userId);
        AddressBook defaultAddress = addressBookService.lambdaQuery()
                .eq(AddressBook::getUserId, userId)
                .eq(AddressBook::getIsDefault, 1)
                .one();
        if (defaultAddress == null){
            return R.error("请选择地址");
        }
        return R.success(defaultAddress);
    }

    /**
     * 为当前用户 添加地址信息
     * @param addressBook 地址信息
     * @return 操作结果
     */
    @PostMapping
    public R<String> saveAddress(@RequestBody AddressBook addressBook){
        //获取当前用户 Id
        Long userId = BaseContext.getCurrentId();
        log.info("id:{}\n添加地址信息:{}", userId, addressBook);
        //添加id
        addressBook.setUserId(userId);
        //添加地址信息
        addressBookService.save(addressBook);
        return R.success("添加地址成功");
    }

    /**
     * 修改地址信息
     * @param addressBook 地址信息
     * @return 修改结果
     */
    @PutMapping
    public R<String> updateAddress(@RequestBody AddressBook addressBook){
        log.info("修改地址信息:{}", addressBook);

        addressBookService.updateById(addressBook);
        return R.success("修改成功");
    }

    /**
     * 根据id 设置默认地址
     * @param addressBook 地址id
     * @return 操作结果
     */
    @PutMapping("default")
    public R<String> defaultAddressById(@RequestBody AddressBook addressBook){
        log.info("将Id为:{} 的地址设置为默认", addressBook);
        //获取当前用户Id
        Long userId = BaseContext.getCurrentId();
        //将当前用户的所有地址 默认值重置
        addressBookService.lambdaUpdate()
                .eq(AddressBook::getUserId, userId)
                .eq(AddressBook::getIsDefault, 1)
                .set(AddressBook::getIsDefault, 0)
                .update(new AddressBook());
        //根据 id设置地址默认值
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return R.success("设置成功");
    }

    /**
     * 根据id删除地址信息
     * @param ids 地址表id
     * @return 操作结果
     */
    @DeleteMapping
    public R<String> deleteAddressById(Long[] ids){
        log.info("删除id为{} 的地址信息", Arrays.asList(ids));
        addressBookService.lambdaUpdate()
                .in(AddressBook::getId, Arrays.asList(ids))
                .remove();

        return R.success("删除成功");
    }

}
